/**
 *  用户状态管理模块
 */
let lifeData = uni.getStorageSync('lifeData') || {};

export default {
	namespaced: true,
	state: {
		/**
		 * js或页面直接调用示例
		 * this.$u.vuex.get('user.userInfo');
		 * js更新示例
		 * this.$u.vuex.set('user.userInfo', data)
		 */
		userInfo: lifeData.user?.userInfo || { },
		token: lifeData.user?.token || '', 
		userId: lifeData.user?.userId || '',
        count: 1
	},
	getters: {
		hasLogin: state => !!state.token,
		getUserInfo: (state) => {
			return state.userInfo;
		}
	},
	mutations: {
		
	},
	actions: {
		// 获取用户信息
		getUserInfo({ state, getters }){
			return new Promise((resolve, reject) => {
				if(!getters.hasLogin){
					return reject()
				}
				uni.$u.request('user.getUserInfo', { 
					user_id: state.userId
				})
				.then(res => {
					uni.$u.vuex.set('user.userInfo', res.data)
					resolve()
				}).catch(err => {
					reject(err)
				})
			})
		},
		// 退出登录清除数据
		exitLogin(){
			uni.$u.vuex.set('user.userInfo', {})
			uni.$u.vuex.set('user.token', "")
			uni.$u.vuex.set('user.userId', "")
		}
	}
}